<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="renderer" content="webkit">
    <title>呼叫云</title>
    <link rel="shortcut icon" href="../img/logox_circle.png">
	<link rel="stylesheet" href="../css/modal_style.css">
    <link rel="stylesheet" href="../css/pintuer.css">
    <link rel="stylesheet" href="../css/admin.css">
    <script src="../js/config.js"></script>
	<script type="text/javascript" charset="utf-8" src="../js/jquery.leanModal.min.js"></script>
	<script>
		$(function(){
			$('#updateRolePrivilege').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
		});
		//获取角色权限列表
		function getRolePrivilegeList1(roleID){
			
			$.ajax({
				url:serviceConfig.role.getRolePrivilegeList,
				type: "get",
				data:{
				     roleID:roleID
				 },
				dataType: "json",
				success:function(data){
					//console.log(data);
					getPrivilegeList(data);
				  },
				error:function(data){
				    console.log(data);
				 }
			});		 			
		}
		//获取权限列表
		function getPrivilegeList(rpdata){
			$.ajax({
				url:serviceConfig.role.getPrivilegeList,
				type: "get",
				data:{
				 },
				dataType: "json",
				success:function(data){
					//console.log(data);
					//console.log(rpdata);
					if(data.flag == '1'){
					 	var privilegeList = data.privilegeList;
					 	var html ="";
					 		html +='<tr>'
		        			+'<td>选择</td>'
		        			+'<td>权限名称</td>'
		        			+'<td>父级权限名称</td>'
		        			+'<td>菜单名称</td>'
					        +'<td>权限编码</td>'
					        +'<td>权限类型</td>'
					        +'<td>权限等级</td>'
					        +'<td>权限顺序</td>'
					        +'<td>备注</td>'
		        			+'</tr>';
			            	for(var i=0;i<privilegeList.length;i++){
				                var showName = privilegeList[i][1];
						 		if(showName == null){
						 			showName = "";
						 		}
						 		
						 		var pshowName = privilegeList[i][8];
						 		if(pshowName == null){
						 			pshowName = "";
						 		}
						 		var moName = privilegeList[i][9];
						 		if(moName == null){
						 			moName = "";
						 		}
						 		var code = privilegeList[i][2];
						 		if(code == null){
						 			code = "";
						 		}
						 		var type = privilegeList[i][3]
						 		if(type == '1'){
						 			type = "菜单";
						 		}else if(type == '2'){
						 			type = "链接";
						 		}else if(type == '3'){
						 			type = "按钮";
						 		}
						 		var level = privilegeList[i][4];
				                 if(level == null){
						 			level = "";
						 		 }
						 		 var privilegeOrder = privilegeList[i][5];
						 		if(privilegeOrder == null){
						 			privilegeOrder = "";
						 		}
						 		var remark = privilegeList[i][6];
						 		if(remark == null){
						 			remark = "";
						 		}
				                 html +='<tr>'
				        			+'<td><input type="checkbox" name="privilegeID" class="class'+privilegeList[i][0]+'" value="'+privilegeList[i][0]+'"/></td>'
				        			+'<td>'+showName+'</td>'
				        			+'<td>'+pshowName+'</td>'
				        			+'<td>'+moName+'</td>'
				        			+'<td>'+code+'</td>'
				        			+'<td>'+type+'</td>'
				        			+'<td>'+level+'</td>'
				        			+'<td>'+privilegeOrder+'</td>'
				        			+'<td>'+remark+'</td>'
				        			+'</tr>';
						 	}
			            }
					 	
					 	$("#treeTable1").html("");
					 	$("#treeTable1").html(html);
					 	
					 	var roleprivilegeList = rpdata.rolePrivilegeList;
					 	for(var i=0;i<privilegeList.length;i++){
					        for(var j=0;j<roleprivilegeList.length;j++){
					        	var rolePrivilegeID = roleprivilegeList[j][0];
					        	var privilegeID = privilegeList[i][0];
					        	if(rolePrivilegeID == privilegeID){
						        	$(".class"+privilegeID).attr("checked","checked");
						        }
					        }
				        }
				  },
				error:function(data){
				    console.log(data);
				 }
			});		 			
		}
		//编辑角色权限
		function updaterp(){
			var roleID = $("#roleID").val();
			var privilegeIDStr = "";
	      	$('input:checkbox[name=privilegeID]:checked').each(function(i){
	       	if(0==i){
	        	privilegeIDStr = $(this).val();
	       	}else{
	        	privilegeIDStr += (","+$(this).val());
	       	}
	      	});
	      	console.log(privilegeIDStr);
	      	$.ajax({
				url:serviceConfig.role.updateRolePrivilege,
				type: "get",
				data:{
				     roleID:roleID,
				     privilegeIDStr:privilegeIDStr
				 },
				dataType: "json",
				success:function(data){
					//console.log(data);
					if(data.flag == '1'){
						alert("编辑成功");
		        	 	window.location.reload();//刷新当前页面.
					}
				  },
				error:function(data){
				    console.log(data);
				 }
			});		 			
		}
	</script>
</head>

<body>
	<div class="panel admin-panel">
			<div class="panel-head"><strong>编辑角色权限</strong></div>
			<table id="treeTable1" class="table table-hover">
        		
           </table>
    </div>
    <button class="button bg-main" style="width:80px;margin-top:30px;margin-left:50px;" id="" onclick="updaterp()">确定</button>
</body></html>